// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/vector/flat_vector.h>

namespace kumo::pollux::generator_spec_examples {

using Sample =
    std::tuple<std::map<int32_t, size_t>, int32_t, int32_t>; // (histogram, null
                                                             // count, total
                                                             // count)

template <typename T>
Sample convertToSample(FlatVector<T>* flat_vector) {
  Sample sample;
  auto& [hist, nulls, total] = sample;
  nulls = 0;
  total = flat_vector->size();
  for (auto i = 0; i < flat_vector->size(); ++i) {
    if (flat_vector->is_null_at(i)) {
      ++nulls;
    } else {
      int32_t val = std::round(flat_vector->value_at(i));
      hist[val]++;
    }
  }
  return sample;
}

template <typename T>
std::string plotVector(FlatVector<T>* flat_vector, const size_t norm = 200) {
  const auto& [hist, nulls, total] = convertToSample(flat_vector);
  std::stringstream sstream;
  for (auto [val, num] : hist) {
    sstream << std::setw(2) << val << ' ' << std::string(num / norm, '*')
            << "\n";
  }
  sstream << "Null ratio = " << ((double)nulls / (double)total) << "\n";
  return sstream.str();
}

} // namespace kumo::pollux::generator_spec_examples
